#! /bin/sh

RUN_PATH=/qqzm/bin
DDNS_CLI=ddns_client
LOG_PATH=/mnt/sd
PROG_LOG_DIR=
ID_CODE_FILE=idcode.data
RUN_PROG="$DDNS_CLI"

function create_log_path()
{
	unset prog
	for prog in $RUN_PROG
	do
		# create log path
		if [ -d $LOG_PATH ]
		then
			PROG_LOG_DIR=$LOG_PATH/log
		else
			PROG_LOG_DIR=`pwd`/log
		fi
		mkdir -p $PROG_LOG_DIR/$prog
	done
}

function run_test_program()
{
	unset prog
	while read line
	do
		id=`echo $line|awk '{print $1}'`
		code=`echo $line|awk '{print $2}'`
		echo "id = $id, code = $code"
		for prog in $RUN_PROG
		do
			if [ -x $prog ]
			then
				echo "run: $prog"
				# redirect stdout and stderr to logfile
				# '&' must be played at the end
				# must flush stdout and stderr in the program
				$RUN_PATH/$prog $id $code &> $PROG_LOG_DIR/$prog/$id.log &
			else
				echo "run '$prog' fail."
			fi
		done
	done < $ID_CODE_FILE
}

function stop_test_program()
{
	unset prog
	for prog in $RUN_PROG
	do
		killall $prog
	done
}

case $1 in
	"start"|"")
		#cp /paramsDir/version/versionMain /paramsDir/version/versionSun
		create_log_path
		run_test_program;;
	"stop")
		stop_test_program;;
	*)
		echo "Usage: `basename $0` [start(default)|stop]";;
esac

exit 0

